package com.edu.eduservice.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.edu.commonutils.JwtUtils;
import com.edu.commonutils.R;
import com.edu.commonutils.user.EduCourseOrder;
import com.edu.eduservice.client.OrderClient;
import com.edu.eduservice.domain.EduCourse;
import com.edu.eduservice.domain.view.CourseQueryVo;
import com.edu.eduservice.domain.view.CourseWebVo;
import com.edu.eduservice.service.EduChapterService;
import com.edu.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @ClassName IndexCourseController
 * @date 2022/9/23 19:09
 * @Version 1.0
 */
@Api(description = "课程前台前端接口")
//@CrossOrigin
@RestController
@RequestMapping("/eduservice/course")
public class IndexCourseController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private OrderClient orderClient;


    @ApiOperation(value = "分页课程列表")
    @PostMapping(value = "/{page}/{limit}")
    @ResponseBody
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseQueryVo courseQuery){
        Page<EduCourse> pageParam = new Page<EduCourse>(page, limit);
        Map<String, Object> map = courseService.pageListWeb(pageParam, courseQuery);
        return  R.ok().data(map);
    }

    @ApiOperation("查询课程详细的方法")
    @PostMapping("/courseinfo/{id}")
    @ResponseBody
    public R courseInfo(@ApiParam(name = "courseId", value = "课程ID", required = true)
                              @PathVariable String id, HttpServletRequest request){

        //查询课程详细的显示数据
        CourseWebVo courseWebVo= this.courseService.selectbyidCourseInfo(id);
        //查询当前课程所有的章节详解
        R r = eduChapterService.getChapterService(id);

        //查询是否被购买
        boolean buyCourse = orderClient.isBuyCourse(JwtUtils.getMemberIdByJwtToken(request), id);
        return r.data("course",courseWebVo).data("isbuy",buyCourse);
    }

    //根据课程id查询课程信息
    @GetMapping("/getDto/{courseId}")
    @ResponseBody
    public EduCourseOrder getCourseInfoDto(@PathVariable String courseId) {
        CourseWebVo courseWebVo = courseService.selectbyidCourseInfo(courseId);
        EduCourseOrder courseInfo = new EduCourseOrder();
        BeanUtils.copyProperties(courseWebVo,courseInfo);
        return courseInfo;
    }

    //把购买数加一
    @PostMapping("/buy/{id}")
    @ResponseBody
    public  R buyId(@PathVariable String id){
        EduCourse course = courseService.getById(id);
        course.setBuyCount(course.getBuyCount()+1);
        boolean b = courseService.updateById(course);

        if(b)
            return R.ok();
        else
            return R.error();
    }

}
